package com.me.customerdemo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.me.customerdemo.dao.OrderMapper;
import com.me.customerdemo.pojo.Order;
import com.me.customerdemo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// @Service注解 可以让Spring框架将所标注的类 初始化为一个对象 并在容器中调用
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    //  使用@Transactional注解标注的方法 是要被纳入事务控制的方法
    // 即当前的这个方法具有原子性 被当做一个事务看待
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean apply(Order order){
        return orderMapper.insert(order) > 0;
    }

    @Override
    public PageInfo<Order> findAll(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orders = orderMapper.selectAll();
        PageInfo<Order> page = new PageInfo<>(orders);
        return page;
    }

    @Override
    public PageInfo<Order> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order> orders = orderMapper.selectByKeyword(keyword);
        PageInfo<Order> page = new PageInfo<>(orders);
        return page;
    }

    @Override
    public Order findById(int id) {
        return orderMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Order order) {
        return orderMapper.updateById(order) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeById(int id) {
        return orderMapper.deleteById(id) > 0;
    }
}
